//
//  TTMusicSongListViewController.m
//  TTMusic
//
//  Created by chenpengyu on 16/3/30.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "TTMusicSongListViewController.h"
#import "TTMusicSongListModel.h"
#import "CPYSongListCollectionViewCell.h"
#import "CPYSongListTypeViewController.h"
#import "CPYSongListDetailViewController.h"
@interface TTMusicSongListViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) TTMusicBaseCollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) UISegmentedControl *seg;
@property (nonatomic, strong) NSString *typeID;
@end

@implementation TTMusicSongListViewController

- (void)viewWillAppear:(BOOL)animated {
//    http://search.dongting.com/songlist/search?s=s200&size=10&hid=6518303829743017&utdid=VfjPsN6N7esDAFG9M0qbxE1S&q=tag%3A最新&net=2&app=ttpod&v=v8.4.1.2016032512&alf=alf702006&tid=0&uid=867992029051448&f=f3030&page=1&imsi=460025418172304
//    http://search.dongting.com/songlist/search?s=s200&size=10&hid=6518303829743017&utdid=VfjPsN6N7esDAFG9M0qbxE1S&q=tag%3A最热&net=2&app=ttpod&v=v8.4.1.2016032512&alf=alf702006&tid=0&uid=867992029051448&f=f3030&page=1&imsi=460025418172304
//    http://search.dongting.com/songlist/search?s=s200&size=10&hid=6518303829743017&utdid=VfjPsN6N7esDAFG9M0qbxE1S&q=tag%3A酒吧&net=2&app=ttpod&v=v8.4.1.2016032512&alf=alf702006&tid=0&uid=867992029051448&f=f3030&page=1&imsi=460025418172304
//    http://api.dongting.com/song/tag/songlist
    [super viewWillAppear:YES];
    [self creatModel];
    
}
- (void)creatModel {
    NSString *url =  @"http://search.dongting.com/songlist/search?s=s200&size=10&hid=6518303829743017&utdid=VfjPsN6N7esDAFG9M0qbxE1S&q=tag%3A最热&net=2&app=ttpod&v=v8.4.1.2016032512&alf=alf702006&tid=0&uid=867992029051448&f=f3030&page=1&imsi=460025418172304";
    [TTMusicAFNetworkTool GETNetWithUrl:url body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
        for (NSDictionary *dic in result[@"data"]) {
            TTMusicSongListModel *songListModel = [[TTMusicSongListModel alloc] init];
            [songListModel setValuesForKeysWithDictionary:dic];
            [_dataSource addObject:songListModel];
        }
        [_collectionView reloadData];
        //        NSLog(@"%@", result);
    } failure:^(NSError *error) {
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
    self.dataSource = [NSMutableArray array];
    // Do any additional setup after loading the view.
    [self creatNavButtonItem];
    [self creatCollectionView];
}
- (void)creatNavButtonItem {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]  initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
}
- (void)leftBarButtonAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)creatCollectionView {
    TTMusicBaseView *headerView = [[TTMusicBaseView alloc] initWithFrame:CGRectMake(0, 0, SysWidth, 40)];
    [self.view addSubview:headerView];
    self.allButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _allButton.frame = CGRectMake(20, 5, 70, 30);
    [_allButton setTitle:self.type forState:UIControlStateNormal];
    _allButton.layer.borderWidth = 3.0f;
    _allButton.layer.borderColor = COLOR(232, 233, 234).CGColor;
    _allButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [_allButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_allButton];
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.itemSize = CGSizeMake(SysWidth / 2, SysWidth / 2 - 20);
    _flowLayout.minimumLineSpacing = 0;
    _flowLayout.minimumInteritemSpacing = 0;
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[TTMusicBaseCollectionView alloc] initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, SHORTHEIGHT - 40) collectionViewLayout:_flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.bounces = NO;
    [_collectionView registerClass:[CPYSongListCollectionViewCell class] forCellWithReuseIdentifier:@"CPYSongListCollectionViewCellIdentifier"];
    [self.view addSubview:_collectionView];
}

- (void)buttonAction:(UIButton *)sender {
    CPYSongListTypeViewController *typeVC = [[CPYSongListTypeViewController alloc] init];
    typeVC.blockOfButtonType = ^(NSString *type){
        if (![self.type isEqualToString:type]) {
            [_allButton setTitle:type forState:UIControlStateNormal];
            [TTMusicAFNetworkTool GETNetWithUrl:[NSString stringWithFormat:@"http://search.dongting.com/songlist/search?size=10&q=tag:最新|%@&page=1", type] body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
                [_dataSource removeAllObjects];
                for (NSDictionary *dic in result[@"data"]) {
                    TTMusicSongListModel *songListModel = [[TTMusicSongListModel alloc] init];
                    [songListModel setValuesForKeysWithDictionary:dic];
                    [_dataSource addObject:songListModel];
                }
                [_collectionView reloadData];
                //        NSLog(@"%@", result);
            } failure:^(NSError *error) {
                
            }];
        }
    
    };
    
    [self.navigationController pushViewController:typeVC animated:YES];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CPYSongListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CPYSongListCollectionViewCellIdentifier" forIndexPath:indexPath];
//    cell.backgroundColor = COLOR(232, 233, 234);
    cell.singListButtonModel = _dataSource[indexPath.item];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CPYSongListDetailViewController *detailVC = [[CPYSongListDetailViewController alloc] init];
    TTMusicSongListModel *songListModel = _dataSource[indexPath.item];
    detailVC.songListId = [NSString stringWithFormat:@"%@", songListModel.quan_id];
    detailVC.imageStr = songListModel.pic_url;
    [self.navigationController pushViewController:detailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//- (void)setType:(NSString *)type {
//    _type = type;
//    [_allButton setTitle:_type forState:UIControlStateNormal];
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
